package com.abhimanyu.bootstrap.tasklets;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import org.apache.log4j.Logger;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.abhimanyu.bootstrap.Util;
import com.abhimanyu.bootstrap.enums.BootstrapConstants;
import com.abhimanyu.bootstrap.util.JobContext;
import com.abhimanyu.dataaccess.IDao;
import com.abhimanyu.dataaccess.model.DollarRate;
import com.abhimanyu.service.ICurrencyRateObserver;
import com.abhimanyu.service.model.DollarRateServiceData;

@Component
public class DollarDataRetrieverTasklet implements Tasklet {
	Logger logger = Logger.getLogger(getClass());
	@Autowired
	private List<ICurrencyRateObserver> currencyRateObserverList;

	@Autowired
	@Qualifier(value = "googleDatastoreDollarRateDao")
	// @Qualifier("inMemoryDollarRateDao")
	private IDao<DollarRate> dao;

	public RepeatStatus execute(StepContribution stepContribution,
			ChunkContext context) throws Exception {
		// List<String> dollarRates = getDollarRatesFromServices();
		List<DollarRateServiceData> dollarRates = getDollarRatesFromServices();

		logger.info("*************************** DOLLAR RATES*************************** ");
		logger.debug(dollarRates);
		JobContext.getInstance().put(BootstrapConstants.DOLLAR_RATES,
				dollarRates);
		return RepeatStatus.FINISHED;
	}

	/*
	 * private List<String> getDollarRatesFromServices() {
	 * System.out.println("STARTING RETRIEVAL"); List<String> dollarRates = new
	 * ArrayList<String>();
	 * System.out.println("currencyRateObserverList.size() : " +
	 * currencyRateObserverList.size()); for (ICurrencyRateObserver service :
	 * currencyRateObserverList) { List<DollarRateService> serviceList =
	 * service.getCurrentDollarRate(); for (DollarRateService dollarRateData :
	 * serviceList) { dollarRates.add(dollarRateData.getServiceName() + " : " +
	 * dollarRateData.getDollarRateData()); } saveDollarRate(service); } return
	 * dollarRates; }
	 */

	private List<DollarRateServiceData> getDollarRatesFromServices() {
		logger.info("STARTING RETRIEVAL");
		List<DollarRateServiceData> serviceList = new ArrayList<DollarRateServiceData>();
		logger.debug("currencyRateObserverList.size() : "
				+ currencyRateObserverList.size());
		
		for (ICurrencyRateObserver service : currencyRateObserverList) {
			serviceList.addAll(service.getCurrentDollarRate());
			try {
			
				saveDollarRate(service);
			} catch (Exception e) {
				logger.error("Error while fetching dollar data from : "
						+ service);
				logger.error(e);
			}
			
		}
		return serviceList;
	}

	private void saveDollarRate(ICurrencyRateObserver service) {

		List<DollarRateServiceData> serviceModelList = service
				.getCurrentDollarRate();
		for (DollarRateServiceData dollarRateService : serviceModelList) {
			DollarRate dollarRate = new DollarRate();
			dollarRate.setDate(Util.offsetTimeZone(new Date(), TimeZone
					.getDefault().getID(), "America/Chicago"));
			dollarRate.setServiceName(dollarRateService.getServiceName());
			dollarRate.setDollarRate(dollarRateService.getDollarRateData());
			logger.info("SAVING : " + dollarRateService);
			dao.save(dollarRate);
		}

	}

	public void setCurrencyRateObserverList(
			List<ICurrencyRateObserver> currencyRateObserverList) {
		this.currencyRateObserverList = currencyRateObserverList;
	}
}
